<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sort</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>Sort</h1>

<p>Arranges a variable's contents in alphabetical, numerical, or random order (optionally removing duplicates).</p>

<pre class="Syntax">Sort, VarName [, Options]</pre>
<h3>Parameters</h3>
<dl>

  <dt>VarName</dt>
  <dd><p>The name of the variable whose contents will be sorted. This cannot be an expression.</p></dd>

  <dt>Options</dt>
  <dd><p>See list below.</p></dd>

</dl>

<h3>Options</h3>
<p><u>A string of zero or more of the following letters (in any order, with optional spaces in between):</u></p>
<p><strong>C</strong>: Case sensitive sort (ignored if the <strong>N</strong> option is also present). If both <strong>C</strong> and <strong>CL</strong> are omitted, the uppercase letters A-Z are considered identical to their lowercase counterparts for the purpose of the sort.</p>
<p><strong>CL</strong> <span class="ver">[v1.0.43.03+]:</span> Case insensitive sort based on the current user's locale. For example, most English and Western European locales treat the letters A-Z and ANSI letters like &Auml; and &Uuml; as identical to their lowercase counterparts. This method also uses a &quot;word sort&quot;, which treats hyphens and apostrophes in such a way that words like &quot;coop&quot; and &quot;co-op&quot; stay together. Depending on the content of the items being sorted, the performance will be 1 to 8 times worse than the default method of insensitivity.</p>
<p><strong>Dx</strong>: Specifies <strong>x</strong> as the delimiter character, which determines where each item in <em>VarName</em> begins and ends. If this option is not present, <strong>x</strong> defaults to linefeed (`n), which correctly sorts <em>VarName</em> if its lines end in either LF (`n) or CR+LF (`r`n).</p>
<p><strong><a name="callback"></a>F MyFunction</strong> <span class="ver">[v1.0.47+]:</span> Uses custom sorting according to the criteria in <em>MyFunction</em> (though sorting takes much longer). Specify the letter &quot;F&quot; followed by optional spaces/tabs followed by the name of a <a href="../Functions.htm">function</a> to be used for comparing any two items in the list. The function must accept two or three parameters. When the function deems the first parameter to be greater than the second, it should return a positive integer; when it deems the two parameters to be equal, it should return 0, &quot;&quot;, or nothing; otherwise, it should return a negative integer. If a decimal point is present in the returned value, that part is ignored (i.e. 0.8 is the same as 0). If present, the third parameter receives the offset (in characters) of the second item from the first as seen in the original/unsorted list (see examples). Finally, the function uses the same global settings (e.g. <a href="StringCaseSense.htm">StringCaseSense</a>) as the Sort command that called it.</p>
<p>Note: the <strong>F</strong> option causes all other options except <strong>D</strong>, <strong>Z</strong>, and <strong>U</strong> to be ignored (though <strong>N</strong>, <strong>C</strong>, and <strong>CL</strong> still affect how <a href="#unique">duplicates</a> are detected). Also, sorting does not occur when the specified function: 1) does not exist; 2) accepts fewer than two parameters; or 3) the first or second parameter is <a href="../Functions.htm#ByRef">ByRef</a>.</p>
<p><strong>N</strong>: Numeric sort: Each item is assumed to be a number rather than a string (for example, if this option is not present, the string 233 is considered to be less than the string 40 due to alphabetical ordering). Both decimal and hexadecimal strings (e.g. 0xF1) are considered to be numeric. Strings that do not start with a number are considered to be zero for the purpose of the sort. Numbers are treated as 64-bit floating point values so that the decimal portion of each number (if any) is taken into account.</p>
<p><strong>Pn</strong>: Sorts items based on character position <strong>n</strong> (do not use hexadecimal for <strong>n</strong>). If this option is not present, <strong>n</strong> defaults to 1, which is the position of the first character. The sort compares each string to the others starting at its <strong>n</strong>th character. If <strong>n</strong> is greater than the length of any string, that string is considered to be blank for the purpose of the sort. When used with option <strong>N</strong> (numeric sort), the string's character position is used, which is not necessarily the same as the number's digit position.</p>
<p><strong>R</strong>: Sorts in reverse order (alphabetically or numerically depending on the other options).</p>
<p><strong>Random</strong>: Sorts in random order. This option causes all other options except <strong>D</strong>, <strong>Z</strong>, and <strong>U</strong> to be ignored (though <strong>N</strong>, <strong>C</strong>, and <strong>CL</strong> still affect how duplicates are detected). Examples:</p>
<pre>Sort, MyVar, Random
Sort, MyVar, Random Z D|</pre>
<p><strong><a name="unique"></a>U</strong>: Removes duplicate items from the list so that every item is unique. <a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to the number of items removed (0 if none). If the <strong>C</strong> option is in effect, the case of items must match for them to be considered identical. If the <strong>N</strong> option is in effect, an item such as 2 would be considered a duplicate of 2.0. If either the <strong>Pn</strong> or <strong>\</strong> (backslash) option is in effect, the entire item must be a duplicate, not just the substring that is used for sorting. If the <strong>Random</strong> or <strong>F/Function</strong> option is in effect, duplicates are removed only if they appear adjacent to each other as a result of the sort. For example, when &quot;A|B|A&quot; is sorted randomly, the result could contain either one or two A's.</p>
<p><strong>Z</strong>: To understand this option, consider a variable that contains RED`nGREEN`nBLUE`n. If the <strong>Z</strong> option is not present, the last linefeed (`n) is considered to be part of the last item, and thus there are only 3 items. But by specifying <strong>Z</strong>, the last `n (if present) will be considered to delimit a blank item at the end of the list, and thus there are 4 items (the last being blank).</p>
<p><strong>\</strong>: Sorts items based on the substring that follows the last backslash in each. If an item has no backslash, the entire item is used as the substring. This option is useful for sorting bare filenames (i.e. excluding their paths), such as the example below, in which the AAA.txt line is sorted above the BBB.txt line because their directories are ignored for the purpose of the sort:</p>
<pre>C:\BBB\AAA.txt
C:\AAA\BBB.txt</pre>
<p>Note: Options <strong>N</strong> and <strong>P</strong> are ignored when the backslash option is present.</p>
<h3>Remarks</h3>
<p>This command is typically used to sort a variable that contains a list of lines, with each line ending in a linefeed character (`n). One way to get a list of lines into a variable is to load an entire file via <a href="FileRead.htm">FileRead</a>.</p>
<p>If <em>VarName</em> is <em>Clipboard</em> and the clipboard contains files (such as those copied from an open Explorer window), those files will be replaced with a sorted list of their filenames. In other words, after the operation, the clipboard will no longer contain the files themselves.</p>
<p><a href="../misc/ErrorLevel.htm">ErrorLevel</a> is changed by this command only when the <strong>U</strong> option is in effect.</p>
<p>The maximum capacity of a variable can be increased via <a href="_MaxMem.htm">#MaxMem</a>.</p>
<p>If a large variable was sorted and later its contents are no longer needed, you can free its memory by making it blank, e.g. <code>MyVar =</code>.</p>
<h3>Related</h3>
<p><a href="FileRead.htm">FileRead</a>, <a href="LoopReadFile.htm">file-reading loop</a>, <a href="LoopParse.htm">parsing loop</a>, <a href="StringSplit.htm">StringSplit</a>, <a href="RegisterCallback.htm">RegisterCallback()</a>, <a href="../misc/Clipboard.htm">clipboard</a>, <a href="_MaxMem.htm">#MaxMem</a></p>
<h3>Examples</h3>
<pre class="NoIndent">MyVar = 5,3,7,9,1,13,999,-4
<strong>Sort</strong> MyVar, N D,  <em>; Sort numerically, use comma as delimiter.</em>
MsgBox %MyVar%   <em>; The result is -4,1,3,5,7,9,13,999</em>

<em>; The following example sorts the contents of a file:</em>
<a href="FileRead.htm">FileRead</a>, Contents, C:\Address List.txt
if not ErrorLevel  <em>; Successfully loaded.</em>
{
    <strong>Sort</strong>, Contents
    FileDelete, C:\Address List (alphabetical).txt
    FileAppend, %Contents%, C:\Address List (alphabetical).txt
    Contents =  <em>; Free the memory.</em>
}

<em>; The following example makes Win+C a hotkey to copy files from an open
; Explorer window and put their sorted filenames onto the clipboard:</em>
#c::
Clipboard = <em>; Must be blank for detection to work.</em>
Send ^c
ClipWait 2
if ErrorLevel
    return
<strong>Sort</strong> Clipboard
MsgBox Ready to be pasted:`n%Clipboard%
return

<em>; The following examples demonstrate custom sorting via a callback function.</em>
MyVar = def`nabc`nmno`nFGH`nco-op`ncoop`ncop`ncon`n
<strong>Sort</strong>, MyVar, F StringSort
StringSort(a1, a2)
{
    return a1 &gt; a2 ? 1 : a1 &lt; a2 ? -1 : 0  <em>; Sorts alphabetically based on the setting of <a href="StringCaseSense.htm">StringCaseSense</a>.</em>
}

MyVar = 5,3,7,9,1,13,999,-4
<strong>Sort</strong>, MyVar, F IntegerSort D,
IntegerSort(a1, a2)
{
    return a1 - a2  <em>; Sorts in ascending numeric order.  This method works only if the difference is never so large as to overflow a signed 64-bit integer.</em>
}

MyVar = 1,2,3,4
<strong>Sort</strong>, MyVar, F ReverseDirection D,  <em>; Reverses the list so that it contains 4,3,2,1</em>
ReverseDirection(a1, a2, offset)
{
    return offset  <em>; Offset is positive if a2 came after a1 in the original list; negative otherwise.</em>
}</pre>

</body>
</html>
